package com.team2.outpatient_sys.web.administrator;

import com.github.pagehelper.PageInfo;
import com.team2.outpatient_sys.pojo.Doctor;
import com.team2.outpatient_sys.pojo.DoctorAndDepartment;
import com.team2.outpatient_sys.pojo.Result;
import com.team2.outpatient_sys.service.AdminDoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 处理管理员对于医护人员的操作请求的Controller
 */
@RestController
@RequestMapping("/admin/doctor")
public class AdminDoctorController {
    @Autowired
    private AdminDoctorService adminDoctorService;

    /**
     * 处理添加新医护人员请求的Controller方法
     * @param doctor post请求体中的doctor数据封装的Doctor对象
     * @return 响应内容，添加新医护人员结果封装的Result对象
     */
    @PostMapping
    public Result addNew(@RequestBody Doctor doctor) {
        boolean isAddSuccess = adminDoctorService.addNew(doctor);
        return isAddSuccess ? new Result(200, "添加新医护人员成功")
                : new Result(500, "添加新医护人员时发生异常");
    }

    /**
     * 处理修改医护人员信息请求的Controller方法
     * 通过Doctor对象逐一修改某一id医护人员信息的所有字段
     * @param doctor put请求体中的doctor数据封装的Doctor对象
     * @return 响应内容，修改医护人员信息结果封装的Result对象
     */
    @PutMapping
    public Result updateWhole(@RequestBody Doctor doctor) {
        boolean isUpdateSuccess = adminDoctorService.updateWhole(doctor);
        return isUpdateSuccess ? new Result(200, "完整医护人员信息修改成功")
                : new Result(500, "修改医护人员信息时发生异常");
    }

    /**
     * 处理根据id删除医护人员信息请求的Controller方法
     * @param doctorId 需要删除的医护人员信息的id
     * @return 响应内容，删除医护人员信息结果封装的Result对象
     */
    @DeleteMapping("/{doctorId}")
    public Result deleteById(@PathVariable Integer doctorId) {
        boolean isDeleteSuccess = adminDoctorService.deleteById(doctorId);
        return isDeleteSuccess ? new Result(200, "根据doctorId删除数据成功")
                : new Result(500, "根据doctorId删除数据时发生异常");
    }

    /**
     * 处理查询所有医护人员信息请求的Controller方法
     * @return 响应内容，查询所有医护人员信息结果封装的Result对象
     *         包含查询到的所有医护人员信息的List集合
     */
    @GetMapping
    public Result getAll() {
        List<Doctor> doctorList = adminDoctorService.getAll();
        Result result = new Result();
        if (doctorList == null) {
            result.setCode(500);
            result.setMsg("所有医护人员信息查询时发生异常");
        } else {
            result.setCode(200);
            result.setMsg("所有医护人员信息查询成功");
            result.setData(doctorList);
        }
        return result;
    }

    /**
     * 处理分页查询医护人员信息请求的Controller方法
     * @param pageNum 查询的分页页码数
     * @param pageSize 每一分页容纳的数据条数
     * @return 响应内容，通过分页查询得到的分页数据结果PageInfo封装的Result对象
     */
    @GetMapping("/{pageNum}/{pageSize}")
    public Result getByPage(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        List<Doctor> doctorList = adminDoctorService.getByPage(pageNum, pageSize);

        Result result = new Result();
        if (doctorList == null) {
            result.setCode(500);
            result.setMsg("医护人员信息分页查询时发生异常，查询结果null");
        } else {
            PageInfo<Doctor> pageInfo = new PageInfo<>(doctorList);
            result.setCode(200);
            result.setMsg("医护人员信息分页查询成功");
            result.setData(pageInfo);
        }
        return result;
    }

    /**
     * 处理分页查询医护人员信息连带查询对应的科室信息请求的Controller方法
     * @param pageNum 查询的分页页码数
     * @param pageSize 每一分页容纳的数据条数
     * @return 响应内容，通过分页查询得到的分页数据结果PageInfo封装的Result对象
     */
    @GetMapping("/andDept/{pageNum}/{pageSize}")
    public Result getByPageAndDept(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        List<DoctorAndDepartment> doctorAndDepartmentList = adminDoctorService.getAndDeptByPage(pageNum, pageSize);

        Result result = new Result();
        if (doctorAndDepartmentList == null) {
            result.setCode(500);
            result.setMsg("医护人员连带科室信息分页查询时发生异常，查询结果null");
        } else {
            PageInfo<DoctorAndDepartment> pageInfo = new PageInfo<>(doctorAndDepartmentList);
            result.setCode(200);
            result.setMsg("医护人员连带科室信息分页查询成功");
            result.setData(pageInfo);
        }
        return result;
    }
}
